package com.mths.client.page;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.Widget;
import com.mths.client.component.Button;
import com.mths.client.component.Div;
import com.mths.client.component.Label;
import com.mths.client.component.TextArea;
import com.mths.client.constant.PageConstant;
import com.mths.client.controller.CompanyController;
import com.mths.client.controller.CompanyControllerAsync;
import com.mths.client.dto.CompanyInfoDTO;
import com.mths.client.dto.CompanySearchDTO;
import com.mths.client.dto.DirectorDTO;
import com.mths.client.dto.InspectorDTO;
import com.mths.client.util.AsyncCall;
import com.mths.client.util.ClientCacheUtil;
import com.mths.client.util.PageUtil;
import com.mths.client.widget.DirectorWidget;
import com.mths.client.widget.InspectorWidget;

public class CompanyInfo extends AbstractPage implements IPage {

	private CompanySearchDTO			companySearchDTO	= new CompanySearchDTO();

	private static CompanyInfoUiBinder	uiBinder			= GWT.create(CompanyInfoUiBinder.class);

	interface CompanyInfoUiBinder extends UiBinder<Widget, CompanyInfo> {
	}

	private final CompanyControllerAsync	companyController	= GWT.create(CompanyController.class);

	@UiField
	Label									companyType, mersisNo, tradeName, companyCenter, committedCapital, paidCapital, phoneNo, faxNo,
			internetAddress, alias;

	@UiField
	TextArea								contactAdress;

	@UiField
	TabPanel								mainTab;

	@UiField
	Div										directorWidget, inspectorWidget;

	@UiField
	Button									updatePageBtn;

	public CompanyInfo() {
		super();
		initWidget(uiBinder.createAndBindUi(this));
		// create event handler
		createEventListener();
		// load component data
		loadCompanyInfo();
		authorizationControl();
	}

	private void authorizationControl() {
		// control user role, for anonymous user remove update button
		List<String> privilegeList = ClientCacheUtil.instance().getPrivilegeItemList();
		String selectedCompanyOid = ClientCacheUtil.instance().getSessionUser().getSelectedCompanyOid();
		List<String> authCompanyList = ClientCacheUtil.instance().getSessionUser().getAuthorizedCompanyOidList();
		if (privilegeList != null && privilegeList.contains("CompanyRegister_SELECT") && authCompanyList.contains(selectedCompanyOid)) {
			updatePageBtn.setVisible(true);
		} else {
			updatePageBtn.removeFromParent();
		}

	}

	private void loadCompanyInfo() {
		// true if user login
		companySearchDTO.setWithObjId(Boolean.FALSE);
		companySearchDTO.setAlias(ClientCacheUtil.instance().getSessionUser().getSelectedCompanyAlias());
		companyController.loadCompanyInfo(companySearchDTO, new AsyncCall<CompanyInfoDTO>() {
			@Override
			public void successCall(CompanyInfoDTO result) {
				loadCompanyData(result);
			}

		});
	}

	private void loadCompanyData(CompanyInfoDTO infoDTO) {
		if (infoDTO == null || infoDTO.getCompanyDTO() == null)
			return;
		companyType.setFieldValue(infoDTO.getCompanyDTO().getCompanyType());
		mersisNo.setFieldValue(infoDTO.getCompanyDTO().getMersisNo());
		tradeName.setFieldValue(infoDTO.getCompanyDTO().getTradeName());
		alias.setFieldValue(infoDTO.getCompanyDTO().getAlias());
		companyCenter.setFieldValue(infoDTO.getCompanyDTO().getCompanyCenter());
		committedCapital.setFieldValue(infoDTO.getCompanyDTO().getCommittedCapital());
		paidCapital.setFieldValue(infoDTO.getCompanyDTO().getPaidCapital());
		phoneNo.setFieldValue(infoDTO.getCompanyDTO().getPhoneNo());
		faxNo.setFieldValue(infoDTO.getCompanyDTO().getFaxNo());
		internetAddress.setFieldValue(infoDTO.getCompanyDTO().getInternetAddress());

		// inspector list
		if (infoDTO.getInspectorDtoList() != null) {
			for (InspectorDTO inspectorDTO : infoDTO.getInspectorDtoList()) {
				inspectorWidget.add(new InspectorWidget(inspectorDTO, Boolean.TRUE));
			}
		}

		// Director list
		if (infoDTO.getDirectorDtoList() != null) {
			for (DirectorDTO directorDTO : infoDTO.getDirectorDtoList()) {
				directorWidget.add(new DirectorWidget(directorDTO, Boolean.TRUE));
			}
		}
	}

	private void createEventListener() {
		mainTab.selectTab(0, true);
		contactAdress.setEnabled(false);

		updatePageBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				PageUtil.navigationManager.controlOpenPageByFUrl(PageConstant.COMPANY_REGISTER);
			}
		});
	}

	@Override
	public String pageName() {
		return messages.CompanyInfo_pageName();
	}
}
